// report-list.js
Page({
    data: {
      allReports: [],
      filteredReports: [],
      typeOptions: ['全部类型', '全面检查', '矫正评估', '洗牙', '根管治疗', '补牙', '智齿拔除'],
      selectedType: 'all',
      selectedTypeIndex: 0,
      selectedDate: '',
      today: ''
    },
  
    onLoad: function () {
      this.setData({
        today: new Date().toISOString().split('T')[0]
      });
      this.fetchReportList();
    },
  
    /**
     * 模拟从数据库获取大量报告数据
     */
    fetchReportList() {
      const titles = [
        '年度口腔健康综合评估报告',
        '牙齿矫正进度追踪与方案调整报告',
        '超声波洗牙后口腔状况报告',
        '根管治疗术后恢复与护理建议报告',
        '龋齿修复（补牙）治疗报告',
        '智齿拔除术前评估与术后指导报告'
      ];
      const doctors = ['张医生', '李医生', '王医生', '刘医生', '陈医生'];
      const tags = [
        { name: '最新', type: 'new' },
        { name: '进行中', type: 'processing' },
        { name: '已完成', type: 'done' }
      ];
      const types = ['全面检查', '矫正评估', '洗牙', '根管治疗', '补牙', '智齿拔除'];
  
      const mockData = [];
      for (let i = 40; i > 0; i--) {
        const randomTypeIndex = Math.floor(Math.random() * types.length);
        const randomTag = tags[Math.floor(Math.random() * tags.length)];
        
        const date = new Date();
        date.setMonth(date.getMonth() - Math.floor(Math.random() * 12)); // 过去一年
        date.setDate(Math.floor(Math.random() * 28) + 1);
        
        mockData.push({
          id: `report${(1000 + i).toString().substring(1)}`,
          title: titles[randomTypeIndex],
          type: types[randomTypeIndex],
          date: date.toISOString().split('T')[0],
          doctor: doctors[Math.floor(Math.random() * doctors.length)],
          tag: randomTag.name,
          tagType: randomTag.type,
          status: randomTag.name === '已完成' ? '已归档' : ''
        });
      }
  
      // 按日期降序排序
      mockData.sort((a, b) => new Date(b.date) - new Date(a.date));
  
      this.setData({
        allReports: mockData,
        filteredReports: mockData
      });
    },
  
    /**
     * 类型筛选器变化时触发
     */
    onTypeChange(e) {
      const index = e.detail.value;
      const type = index === '0' ? 'all' : this.data.typeOptions[index];
      this.setData({
        selectedTypeIndex: index,
        selectedType: type
      }, () => {
        this.filterReports();
      });
    },
  
    /**
     * 日期筛选器变化时触发
     */
    onDateChange(e) {
      this.setData({
        selectedDate: e.detail.value
      }, () => {
        this.filterReports();
      });
    },
  
    /**
     * 重置所有筛选条件
     */
    onReset() {
      this.setData({
        selectedType: 'all',
        selectedTypeIndex: 0,
        selectedDate: ''
      }, () => {
        this.filterReports();
      });
    },
  
    /**
     * 核心筛选逻辑函数
     */
    filterReports() {
      const { allReports, selectedType, selectedDate } = this.data;
      
      let results = [...allReports];
  
      if (selectedType !== 'all') {
        results = results.filter(report => report.type === selectedType);
      }
  
      if (selectedDate) {
        results = results.filter(report => report.date === selectedDate);
      }
  
      this.setData({
        filteredReports: results
      });
    },
  
    /**
     * 跳转到详情页
     */
    goToDetail(e) {
      const reportId = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/business/report-detail/report-detail'
      });
    }
  });